﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    public class ReviewCaseOptController : ApiController
    {
        /// <summary>
        /// 获取审批方案列表
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|方案所属公司代码|方案所属部门代码|方案id（0=所有）|有效标志（Y-有效；N-停用；空值=所有）|方案模板类别（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/reviewcaseopt")]
        public RespReviewCase GetReviewCase(string key)
        {
            RespReviewCase resp = new RespReviewCase();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 8)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCaseComcd = strsKeyOriginal[3];
            string strDeptcd = strsKeyOriginal[4];
            string strValid = strsKeyOriginal[6];
            string strCasetp = strsKeyOriginal[7];
            int iCaseid;
            if (!int.TryParse(strsKeyOriginal[5], out iCaseid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetRevCase(strCaseComcd, strDeptcd, iCaseid, strValid, strCasetp);
            return resp;
        }

        /// <summary>
        /// 更新审批方案信息
        /// </summary>
        /// <param name="_reqInfo">审批方案更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/reviewcaseopt")]
        public RespFinaNullResultInfo UpdateReviewCase(ReqReviewCase _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateRevCase(_reqInfo.memberid, _reqInfo.revcase);
            return resp;
        }

        /// <summary>
        /// 获取审批方案明细
        /// eg.：0000000001|TST1|WEB|1
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|方案id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/reviewcasedtlopt")]
        public RespReviewCaseDetail GetReviewCaseDetail(string key)
        {
            RespReviewCaseDetail resp = new RespReviewCaseDetail();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iCaseid;
            if (!int.TryParse(strsKeyOriginal[3], out iCaseid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetRevCaseDetail(iCaseid);
            return resp;
        }

        /// <summary>
        /// 操作审批方案明细
        /// 明细流水号与序号不支持修改，只能增删
        /// </summary>
        /// <param name="_reqInfo">审批方案明细操作请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/reviewcasedtlopt")]
        public RespFinaNullResultInfo UpdateReviewCaseDetail(ReqReviewCaseDetail _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateRevCaseDetail( _reqInfo.revdtl);
            return resp;
        }

    }
}
